<?php

class protocolObject
{
	public $objectType = "protocolObject";
	public $exitCode = 2;
	public $exitText = "";
	
	public function createTableQuery($queryColumns,$queryTarget,$viewName)
	{
		$this->queryColumns = $queryColumns;
		$this->queryTarget = $queryTarget;
		$this->viewName = $viewName;
	}
	
	public function giveExitStatus($exitCode,$exitStatus)
	{
		$this->exitCode = $exitCode;
		$this->exitText = $exitStatus;
	}
}

class User
{
	public $exitCode = 2;
	public $exitText = "";
	public $userName = "";
	public $userId = "";
	public $userPass = "";
	private $userVerified = false;
	
	public function giveExitStatus($exitCode,$exitStatus)
	{
		$this->exitCode = $exitCode;
		$this->exitText = $exitStatus;
	}
	
	public function verifyUser()
	{
		if($this->userName != "")
		{
			if($this->userPass != "")
			{
				
			}
			else
			{
				$this->giveExitStatus(1,"A Password was not provided");
			}
		}
		else
		{
			$this->giveExistStatus(1,"User was instatiated");
		}
	}
	
	public function checkPassword()
	{
		if($this->checkSet())
		{
			if($this->userValFlag == 1)
			{
				
				$this->passwordHashQuery = "select userPassword from user where userName = " . crowFather($this->userName,6) . ";";
				$this->passwordHashAnswer = crowFather($this->passwordHashQuery,5);
				if(hash('sha256',$this->userPass) == $this->passwordHashAnswer)
				{
					crowFather("update user set userLastLogin = now() where userName =" . crowFather($this->userName,6) . ";",3);
					return true;
				}
				else
				{
					return false;
				}
			}
		}
	}
	
	public function addUser()
	{
		#check if user exists
		if($this->checkSet())
		{
			$userExists = crowFather("select count(*) from user where userName = " . $this->userName . ";",5);
			if($userExists == 0)
			{
				#supported in PHP5.5
				#crowFather("insert into user(userName,userPassword) values('" . $this->userName . "','" . password_hash($this->userPassword,PASSWORD_BCRYPT) . "');",3);
				
				crowFather("insert into user(userName,userPassword) values('" . $this->userName . "','" . hash('sha256',$this->userPass) . "');",3);
				$this->giveExitStatus(0,"The user was added successfully");
			}
			else
			{
				$this->giveExitStatus(1,"The user already exists");
			}
		}
		else
		{
			$this->giveExitStatus(1,"The user was not defined");
		}
		#if dosen't exist , create it
		#exit with failure if user exists
	}
	
	private function checkSet()
	{
	
		$this->giveExitStatus(1,"User was not instatiated");
		$this->userValFlag = false;
		
		if($this->userName != "")
		{
			if($this->userPass == "")
			{
				$this->giveExitStatus(1,"A Password was not provided");
				$this->userValFlag = false;
			}
			else
			{
				$this->giveExitStatus(0,"Ready");
				$this->userValFlag = true;
				$this->checkUnique();
				$this->userVerMode = 2;
				return true;
			}
		}
		if($this->userId != "")
		{
			if($this->userPass == "")
			{
				$this->giveExitStatus(1,"A Password was not provided");
				$this->userValFlag = false;
			}
			else
			{
				$this->giveExitStatus(0,"Ready");
				$this->userValFlag = true;
				$this->checkUnique();
				$this->userVerMode = 1;
				return true;
			}
		}
		
	}
	
	public function checkUnique()
	{
		$this->isUnique = false;
		$this->giveExitStatus(0,"Duplicated");
		
		if($this->userId != "" && crowFather("select count(*) from user where userId = " . $this->userId . ";",5) == 0)
		{
			$this->isUnique = true;
			$this->giveExitStatus(0,"Unique");
		}
		
		if($this->userName != "" && crowFather("select count(*) from user where userName = " . $this->userName . ";",5) == 0)
		{
			$this->giveExitStatus(0,"Unique");
			$this->isUnique = true;
		}
		
	}
	
}

function sendDebugInfo($outputObject)
{
	$sqlApprovedObject = urlencode(json_encode($outputObject));
	if(!isset($outputObject->messageSender))
	{
		crowFather("insert into debug(senderId,returnObject) values(3,'" . $sqlApprovedObject . "');",3);
	}
	else
	{
		crowFather("insert into debug(senderId,returnObject) values(" . $outputObject->messageSender . ",'" . $sqlApprovedObject . "');",3);
	}
}

class messageQuery
{
	
	function getResultArray($outputObject)
	{
		$this->testData = "";
		$this->queryColumns = $outputObject->queryColumns;
		$this->queryTarget = $outputObject->queryTarget;
		$this->viewName = $outputObject->viewName;
		$this->dataInputQuery = "select ";
		$counter = 1;
		
		foreach($this->queryColumns as $columnName)
		{
			if($counter < sizeof($this->queryColumns))
			{
				$this->dataInputQuery .= $columnName . ",";
			}
			else
			{
				$this->dataInputQuery .= $columnName;
			}
			++$counter;
		}
		
		$this->dataInputQuery .= " from " . $this->viewName . " where ";
		
		$foreachCounter = 1;
		
		foreach($this->queryTarget as $whereElements)
		{
		
			if(!$foreachCounter % 2)
			{
				if($foreachCounter > 2)
				{
					#$this->dataInputQuery .= " AND ";
					$this->dataInputQuery .= $foreachCounter . " AND ";
				}
				$this->dataInputQuery .= $foreachCounter .  $this->queryTarget[$foreachCounter - 1] . " = ";
			}
			else
			{
				$this->dataInputQuery .= $foreachCounter .  $this->queryTarget[$foreachCounter - 1] . " ";
			}
			
			++$foreachCounter;
		}
		
		$this->dataInputQuery .= ";";
		
		$this->queryArray = crowFather($this->dataInputQuery,1);
	}
	
	function getTable()
	{	
		if(isset($this->queryArray))
		{
			#Begin the table HTML
			$this->returnData = "<table class='tableInstance'><tr class='headingRow'>";
			$counter = 0;
			
			foreach ($this->queryColumns as $columnName)
			{
				#Generate the table headings
				$this->returnData .= "<td class='headingRowContent'>" . $columnName . "</td>";
				++$counter;
			}
			
			#Close off the table heading section
			$this->returnData .= "</tr>";
			foreach($this->queryArray as $messageQueryRow)
			{
				#Begin a new row
				$this->returnData .= "<tr class='tableContentRow'>";
				$columnID = 0;
				
				foreach ($messageQueryRow as $messageQueryCell)
				{
					#Generate the next cell
					switch($columnID)
					{
						#Add cell contents
						case 1:
							$this->returnData .= "<td class='tableContentCell'>" . $messageQueryCell . "</td>";
							break;
						case 3:
							$this->returnData .= "<td class='tableContentCell'>" . $messageQueryCell . "</td>";
							break;
					}
					++$columnID;
				}
				
				$this->returnData .= "</tr>";
			}
			
			$this->returnData .= "</table>";
			$this->giveExitStatus(0,"Action completed successfully");
		}
		else
		{
			$this->giveExitStatus(1,"Run getResultArray first!");
		}
	}
	
	function getRawData()
	{
		$AR_messageQuery = crowFather("select messageSender,messageText from message where queryMessageView = '" . $outputObject->messageRecipient . "';",1);
		
		foreach($AR_messageQuery as $messageQueryRow)
		{
			$outputObject->returnData .= "<tr>";
			$columnID = 0;
			
			foreach ($messageQueryRow as $messageQueryCell)
			{
				$outputObject->returnData .= "<td>";
				switch($columnID)
				{
					case 1:
						$outputObject->returnData .= $messageQueryCell;
						break;
					case 3:
						$outputObject->returnData .= $messageQueryCell;
						break;
				}
				$outputObject->returnData .= "</td>";
				++$columnID;
			}
			
			$outputObject->returnData .= "</tr>";
		}
		
		$outputObject->returnData .= "</table>";
		$outputObject->giveExitStatus(0,"Action completed successfully");
	}
	
	function giveExitStatus($exitCode,$exitText)
	{
		$this->exitCode = $exitCode;
		$this->exitText = $exitText;
	}
}

function checkUsersExist($outputObject)
{
	if((isset($outputObject->messageSender) || isset($outputObject->messageSenderID)) && (isset($outputObject->messageRecipient) || $outputObject->messageRecipientID))
	{
		$outputObject->userExistBool = array();
		$outputObject->userDupBool = array();
		$outputObject->senderQueryResult = crowFather("select count(*) from user where userId = '" . $outputObject->messageSender . "';",5);
		$outputObject->recipientQueryResult = crowFather("select count(*) from user where userId = '" . $outputObject->messageRecipient . "';",5);
		
		switch($outputObject->senderQueryResult)
		{
			case 0:
				#No user exists
				$outputObject->userExistBool["sender"] = false;
				$outputObject->userDupBool["sender"] = false;
				break;
			case 1:
				#User exists
				$outputObject->userExistBool["sender"] = true;
				$outputObject->userDupBool["sender"] = false;
				break;
		}
		if($outputObject->senderQueryResult > 1)
		{
			#User exists but is duplicated
			$outputObject->userExistBool["sender"] = true;
			$outputObject->userDupBool["sender"] = true;
		}
		
		switch($outputObject->recipientQueryResult)
		{
			case 0:
				#No user exists
				$outputObject->userExistBool["recipient"] = false;
				$outputObject->userDupBool["recipient"] = false;
				break;
			case 1:
				#User exists
				$outputObject->userExistBool["recipient"] = true;
				$outputObject->userDupBool["recipient"] = false;
				break;
		}
		if($outputObject->recipientQueryResult)
		{
			#User exists but is duplicated
			$outputObject->userExistBool["recipient"] = true;
			$outputObject->userDupBool["recipient"] = true;
		}
	}
	
	else
	{
		#if this dosen't evaluate , no user is set , so make return 0
		
	}
}
?>